package cn.wolfcode.trip.base.service.impl;


import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelLike;
import cn.wolfcode.trip.base.mapper.TravelLikeMapper;
import cn.wolfcode.trip.base.query.LikeQueryObject;
import cn.wolfcode.trip.base.service.ITravelLikeService;
import cn.wolfcode.trip.base.util.TimeUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class TravelLikeServiceImpl implements ITravelLikeService {

    @Autowired
    private TravelLikeMapper travelLikeMapper;

    @Override
    public TravelLike queryLike(Long travelId, Long userId) {
        return travelLikeMapper.selectLike(travelId,userId);
    }

    @Override
    public void save(TravelLike entity) {
        travelLikeMapper.insert(entity);
    }

    @Override
    public void deleteLike(Long travelId, Long userId) {
        travelLikeMapper.deleteLike(travelId,userId);
    }

    @Override
    public Object countUnReadLikes(LikeQueryObject qo) {
        //判断是否 查出 为null
        Integer countUnReadSystemMessages = travelLikeMapper.countUnReadLikes(qo);
        if(countUnReadSystemMessages == null){
            return 0;
        }else {
            return  countUnReadSystemMessages;
        }
    }

    public PageInfo<?> selectUserAcceptForList(LikeQueryObject qo) {
        qo.setPageSize(7);
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Map> list = travelLikeMapper.selectUserAcceptForList(qo);
        for (Map map : list) {
            Date createTime = (Date)map.get("createtime");
            //格式化
            String Object = TimeUtil.DateformatTime(createTime);
            map.put("createtime",Object);
            //处理 回显信息
            Object title = map.get("title");
            Object travelId = map.get("travelId");
            String message = "赞了你的游记\""+title+"\"";
//            String message = nickName+",赞了你的游记\""+"<a class='unreadHref' href='/travelContent.html?id="+travelId+"'>"+title+"</a>"+"\"";
            map.put("message",message);
        }


        return new PageInfo<>(list);
    }
    public PageInfo<?> selectUserSendForList(LikeQueryObject qo) {
        qo.setPageSize(6);
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Map> list = travelLikeMapper.selectUserSendForList(qo);
        for (Map map : list) {
            Date createTime = (Date)map.get("createtime");
            //格式化
            String Object = TimeUtil.DateformatTime(createTime);
            map.put("createtime",Object);
            //处理 回显信息
            Object title = map.get("title");
            Object authorNickName = map.get("authorNickName");
            String message = "你赞了\""+authorNickName +"\"的游记";
            map.put("message",message);
        }
        return new PageInfo<>(list);
    }


    @Override
    public void updateState(Long authorId) {
        travelLikeMapper.updateState(authorId);
    }

    @Override
    public Map maxLike(LikeQueryObject qo) {
        return travelLikeMapper.maxLike(qo);
    }

    @Override
    public Integer countOthersLike(LikeQueryObject qo) {
        //判断是否 查出 为null
        Integer countUnReadSystemMessages = travelLikeMapper.countOthersLike(qo);
        if(countUnReadSystemMessages == null){
            return 0;
        }else {
            return  countUnReadSystemMessages;
        }
    }

    @Override
    public Integer countLike(LikeQueryObject qo) {
        //判断是否 查出 为null
        Integer countUnReadSystemMessages = travelLikeMapper.countLike(qo);
        if(countUnReadSystemMessages == null){
            return 0;
        }else {
            return  countUnReadSystemMessages;
        }
    }


}
